import { createWebHistory, createRouter } from "vue-router";
// 导入语言翻译包
import { i18n } from "@/lang";
const t = i18n.global.t;
/* Layout */
export const Layout = () => import("@/layout/index.vue");

// 公共路由
export const constantRoutes = [
  {
    path: "/redirect",
    component: Layout,
    hidden: true,
    children: [
      {
        path: "/redirect/:path(.*)",
        component: () => import("@/views/index.vue"),
      },
    ],
  },

  {
    path: "/",
    component: Layout,
    redirect: "/index",
    hidden: false,
    children: [
      {
        path: "/",
        component: () => import("@/views/README.md"),
        name: "Index",
        meta: { title: t("routerSetting.introduce") },
      },
      {
        path: "/button",
        component: () => import("@/views/button/README.md"),
        name: "Button",
        meta: { title: "按钮组件" },
      },
      {
        path: "/autoComplete",
        component: () => import("@/views/autoComplete/README.md"),
        name: "AutoComplete",
        meta: { title: "输入自动补全组件" },
      },
      {
        path: "/form",
        component: () => import("@/views/form/README.md"),
        name: "Form",
        meta: { title: t("routerSetting.FormComponent") },
      },
      {
        path: "/table",
        component: () => import("@/views/table/README.md"),
        name: "Table",
        meta: { title: t("routerSetting.TableComponent") },
      },
      {
        path: "/dialog",
        component: () => import("@/views/dialog/README.md"),
        name: "Dialog",
        meta: { title: "弹窗组件" },
      },
      {
        path: "/upload",
        component: () => import("@/views/upload/README.md"),
        name: "Upload",
        meta: { title: "上传文件组件" },
      },
      {
        path: "/tree",
        component: () => import("@/views/tree/README.md"),
        name: "Tree",
        meta: { title: "树形组件" },
      },
      {
        path: "/transfer",
        component: () => import("@/views/transfer/README.md"),
        name: "Transfer",
        meta: { title: "穿梭框组件" },
      },

      {
        path: "/pagination",
        component: () => import("@/views/pagination/README.md"),
        name: "Pagination",
        meta: { title: "分页组件" },
      },
      {
        path: "/empty",
        component: () => import("@/views/empty/README.md"),
        name: "Empty",
        meta: { title: "空状态组件" },
      },
      {
        path: "/desc",
        component: () => import("@/views/desc/README.md"),
        name: "Desc",
        meta: { title: "描述列表组件" },
      },
      {
        path: "/echarts",
        component: () => import("@/views/echarts/README.md"),
        name: "Echarts",
        meta: { title: "可视化组件" },
      },
    ],
  },
];

const router = createRouter({
  history: createWebHistory(import.meta.env.VITE_APP_PUBLIC_PATH || "/"),
  routes: constantRoutes,
  // 刷新时，滚动条位置还原
  //scrollBehavior: () => ({ left: 0, top: 0 }),
  scrollBehavior(to, from, savedPosition) {
    if (savedPosition) {
      return savedPosition;
    } else {
      return { top: 0 };
    }
  },
});

export default router;
